/*
 * Task.cpp
 *
 *  Created on: 19.11.2012
 *      Author: Lapchinskiy
 */
#include "BuildConfig.h"
#include "Block.h"
#include "Global_types.h"
#include "Logging.h"
#include "ArrayList.h"
#include "ConditionVariable.h"
#include "Condition.h"
#ifdef TERMINAL_CONTROL_ENABLE
#include "CommandHandler.h"
#endif //TERMINAL_CONTROL_ENABLE
#include "Task.h"

//using namespace std;
//Task::Task(char* name, ErrorCode (*callBack)(void *args),
//		Condition<int> *conditonToRun) {
//	Task(name, callBack);
//	this->conditionToRun = conditonToRun;
//}
#ifdef GLOBAL_LOGGING_ENABLE
ModuleType Task::moduleType = TASK;
#endif //GLOBAL_LOGGING_ENABL
uint8_t Task::taskCounter = 0;

Block* Task::step(void)
  {
    if ((status == ENABLED)
        && ((conditionToRun == 0) || (conditionToRun->check() == true)))
      {
#ifdef GLOBAL_LOGGING_ENABLE
#ifdef TASK_LOGGING_ENABLE
        //Log(&logrec, errorCode, (char*) "started!");
#endif //TASK_LOGGING_ENABLE
#endif //GLOBAL_LOGGING_ENABLE
        errorCode = CallbackRun(callbackArgs);
        return nextBlock;
      }
    return this;
  }
ErrorCode Task::CallbackRun(void *args)
  {
#ifdef GLOBAL_LOGGING_ENABLE
#ifdef TASK_LOGGING_ENABLE
    Log(&logrec, errorCode, (char*) "started!");
#endif //GLOBAL_LOGGING_ENABLE
#endif //GLOBAL_LOGGING_ENABLE
    return callbackFunc(args);
  }
Task::Task(char* name, ErrorCode (*callBack)(void *args), void *args,
    TaskStatus enabled)
  {
    this->status = enabled;
    this->id = Task::taskCounter++;
    this->callbackFunc = callBack;
    this->conditionToRun = NULL;
    this->callbackArgs = args;
    this->nextBlock = this;
    errorCode = NO_ERR;
#ifdef GLOBAL_LOGGING_ENABLE
#ifdef GLOBAL_LOGGING_ENABLE
    logrec.module = TASK;
    logrec.objectName = name;
    logrec.logEnable = true;
#endif //GLOBAL_LOGGING_ENABLE
#endif //GLOBAL_LOGGING_ENABLE
#ifdef GLOBAL_LOGGING_ENABLE
    Log(&logrec, errorCode, (char*) "created!");
#endif //GLOBAL_LOGGING_ENABLE
#ifdef TERMINAL_CONTROL_ENABLE
    CommandHandler::RegisterTask(this);
#endif //TERMINAL_CONTROL_ENABLE
  }

Task::Task()
  {

  }

ErrorCode Task::setNexBlock(Block* nextBlock)
  {
    this->nextBlock = nextBlock;
    return errorCode;
  }

ErrorCode Task::Enable(void)
  {
    status = ENABLED;
    return errorCode;
  }

ErrorCode Task::Disable(void)
  {
    status = DISABLED;
    return errorCode;
  }

TaskStatus Task::GetStatus(void)
  {
    return status;
  }

Task::~Task()
  {

  }
